package com.medusa.gruul.payment.service.config;

import com.egzosn.pay.spring.boot.core.PayServiceConfigurer;
import com.egzosn.pay.spring.boot.core.configurers.MerchantDetailsServiceConfigurer;
import com.egzosn.pay.spring.boot.core.configurers.PayMessageConfigurer;
import com.egzosn.pay.spring.boot.core.merchant.PaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.AliPaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.PaymentPlatforms;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.WxPaymentPlatform;
import com.medusa.gruul.payment.service.handler.pay.notify.AliPayMessageHandler;
import com.medusa.gruul.payment.service.handler.pay.notify.WxPayMessageHandler;
import com.medusa.gruul.payment.service.interceptor.AliPayMessageInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * 支付服务配置
 *
 *
 * @author xiaoq
 * @ Description 支付服务配置
 * @date 2022-07-12 16:47
 */
@Configuration
@RequiredArgsConstructor
public class MerchantPayServiceConfig implements PayServiceConfigurer {

    private final WxPayMessageHandler wxPayMessageHandler;
    private final JdbcTemplate jdbcTemplate;
    private final AutowireCapableBeanFactory spring;
    private final AliPayMessageHandler aliPayMessageHandler;

    @Override
    public void configure(MerchantDetailsServiceConfigurer merchantDetailsServiceConfigurer) {
        merchantDetailsServiceConfigurer.jdbc()
                .cache(false)
                .template(jdbcTemplate);
    }

    @Override
    @SuppressWarnings("rawtypes")
    public void configure(PayMessageConfigurer configurer) {
        PaymentPlatform aliPaymentPlatform = PaymentPlatforms.getPaymentPlatform(AliPaymentPlatform.PLATFORM_NAME);
        configurer.addHandler(aliPaymentPlatform, aliPayMessageHandler);
        configurer.addInterceptor(aliPaymentPlatform, spring.getBean(AliPayMessageInterceptor.class));
        configurer.addHandler(PaymentPlatforms.getPaymentPlatform(WxPaymentPlatform.PLATFORM_NAME), wxPayMessageHandler);
    }
}
